import { createRouter, createWebHashHistory } from 'vue-router'
import login from '@/views/login/login.vue'
import frame from '@/views/main/frame.vue'
import myabsent from '@/views/absent/my.vue'
import subabsent from '@/views/absent/sub.vue'
import publish from '@/views/inform/publish.vue'
import list from '@/views/inform/list.vue'
import detail from '@/views/inform/detail.vue'
import { useAuthStore } from '@/stores/auth'
import staffadd from '@/views/staff/add.vue'
import stafflist from '@/views/staff/list.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'frame',
      component: frame,
      children: [
        { path: '/absent/my', name: 'myabsent', component: myabsent },
        { path: '/absent/sub', name: 'subabsent', component: subabsent },
        { path: '/inform/publish', name: 'inform_publish', component: publish },
        { path: '/inform/list', name: 'inform_list', component: list },
        { path: '/inform/detail/:pk', name: 'inform_detail', component: detail },
        { path: '/staff/add', name: 'staff_add', component: staffadd },
        { path: '/staff/list', name: 'staff_list', component: stafflist },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

router.beforeEach((to, from) => {
  // 判断用户是否登录
  // 访问的页面是不是登录页，跳转到登录页
  const authStore = useAuthStore()
  if (!authStore.is_logined && to.name != "login") {
    return { name: "login" }
  }
})

export default router
